import 'package:intl/intl.dart';

extension StringExtension on String? {
  /// 空安全
  String get nullSafe => this ?? '';

  /// 是否为空
  bool get isEmptyOrNull => this == null || this!.isEmpty;

  /// 是否不为空
  bool get isNotNullAndEmpty => this != null && this!.isNotEmpty;

  /// 为国际化做准备,如果要考虑国际化在这里根据选择的语言，返回不同的翻译
  String get tr => this ?? '';

  /// 时间戳字符串转换为标准格式 (yyyy-MM-dd HH:mm:ss)
  String? get toStandardDateTime {
    if (isEmptyOrNull) return null;
    try {
      final timestamp = int.parse(this!);
      return DateFormat('yyyy-MM-dd HH:mm:ss')
          .format(DateTime.fromMillisecondsSinceEpoch(timestamp));
    } catch (e) {
      return null;
    }
  }

  /// 时间戳字符串转换为日期格式 (yyyy-MM-dd)
  String? get toDate {
    if (isEmptyOrNull) return null;
    try {
      final timestamp = int.parse(this!);
      return DateFormat('yyyy-MM-dd')
          .format(DateTime.fromMillisecondsSinceEpoch(timestamp));
    } catch (e) {
      return null;
    }
  }

  /// 时间戳字符串转换为时间格式 (HH:mm:ss)
  String? get toTime {
    if (isEmptyOrNull) return null;
    try {
      final timestamp = int.parse(this!);
      return DateFormat('HH:mm:ss')
          .format(DateTime.fromMillisecondsSinceEpoch(timestamp));
    } catch (e) {
      return null;
    }
  }

  /// 时间戳字符串转换为短时间格式 (HH:mm)
  String? get toShortTime {
    if (isEmptyOrNull) return null;
    try {
      final timestamp = int.parse(this!);
      return DateFormat('HH:mm')
          .format(DateTime.fromMillisecondsSinceEpoch(timestamp));
    } catch (e) {
      return null;
    }
  }

  /// 时间戳字符串转换为年月格式 (yyyy-MM)
  String? get toYearMonth {
    if (isEmptyOrNull) return null;
    try {
      final timestamp = int.parse(this!);
      return DateFormat('yyyy-MM')
          .format(DateTime.fromMillisecondsSinceEpoch(timestamp));
    } catch (e) {
      return null;
    }
  }

  /// 时间戳字符串转换为中文日期格式 (yyyy年MM月dd日)
  String? get toChineseDate {
    if (isEmptyOrNull) return null;
    try {
      final timestamp = int.parse(this!);
      return DateFormat('yyyy年MM月dd日')
          .format(DateTime.fromMillisecondsSinceEpoch(timestamp));
    } catch (e) {
      return null;
    }
  }

  /// 时间戳字符串转换为中文日期时间格式 (yyyy年MM月dd日 HH:mm:ss)
  String? get toChineseDateTime {
    if (isEmptyOrNull) return null;
    try {
      final timestamp = int.parse(this!);
      return DateFormat('yyyy年MM月dd日 HH:mm:ss')
          .format(DateTime.fromMillisecondsSinceEpoch(timestamp));
    } catch (e) {
      return null;
    }
  }

  /// 时间戳字符串转换为相对时间（刚刚、x分钟前、x小时前、昨天、x天前）
  String? get toRelativeTime {
    if (isEmptyOrNull) return null;
    try {
      final timestamp = int.parse(this!);
      final dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
      final now = DateTime.now();
      final difference = now.difference(dateTime);

      if (difference.inSeconds < 60) {
        return '刚刚';
      } else if (difference.inMinutes < 60) {
        return '${difference.inMinutes}分钟前';
      } else if (difference.inHours < 24) {
        return '${difference.inHours}小时前';
      } else if (difference.inDays == 1) {
        return '昨天';
      } else if (difference.inDays < 7) {
        return '${difference.inDays}天前';
      } else {
        return toDate;
      }
    } catch (e) {
      return null;
    }
  }

  /// 标准格式字符串转换为时间戳字符串 (yyyy-MM-dd HH:mm:ss)
  String? get fromStandardDateTime {
    if (isEmptyOrNull) return null;
    try {
      final dateTime = DateFormat('yyyy-MM-dd HH:mm:ss').parse(this!);
      return dateTime.millisecondsSinceEpoch.toString();
    } catch (e) {
      return null;
    }
  }

  /// 日期字符串转换为时间戳字符串 (yyyy-MM-dd)
  String? get fromDate {
    if (isEmptyOrNull) return null;
    try {
      final dateTime = DateFormat('yyyy-MM-dd').parse(this!);
      return dateTime.millisecondsSinceEpoch.toString();
    } catch (e) {
      return null;
    }
  }

  /// 时间字符串转换为时间戳字符串 (HH:mm:ss)
  String? get fromTime {
    if (isEmptyOrNull) return null;
    try {
      final now = DateTime.now();
      final time = DateFormat('HH:mm:ss').parse(this!);
      final dateTime = DateTime(
        now.year,
        now.month,
        now.day,
        time.hour,
        time.minute,
        time.second,
      );
      return dateTime.millisecondsSinceEpoch.toString();
    } catch (e) {
      return null;
    }
  }

  /// 年月字符串转换为时间戳字符串 (yyyy-MM)
  String? get fromYearMonth {
    if (isEmptyOrNull) return null;
    try {
      final dateTime = DateFormat('yyyy-MM').parse(this!);
      return dateTime.millisecondsSinceEpoch.toString();
    } catch (e) {
      return null;
    }
  }

  /// 中文日期字符串转换为时间戳字符串 (yyyy年MM月dd日)
  String? get fromChineseDate {
    if (isEmptyOrNull) return null;
    try {
      final dateTime = DateFormat('yyyy年MM月dd日').parse(this!);
      return dateTime.millisecondsSinceEpoch.toString();
    } catch (e) {
      return null;
    }
  }

  /// 中文日期时间字符串转换为时间戳字符串 (yyyy年MM月dd日 HH:mm:ss)
  String? get fromChineseDateTime {
    if (isEmptyOrNull) return null;
    try {
      final dateTime = DateFormat('yyyy年MM月dd日 HH:mm:ss').parse(this!);
      return dateTime.millisecondsSinceEpoch.toString();
    } catch (e) {
      return null;
    }
  }
  DateTime? get toDateTime {
    if (isEmptyOrNull) return null;
    try {
      final timestamp = int.parse(this!);
      return DateTime.fromMillisecondsSinceEpoch(timestamp);
    } catch (e) {
      return null;
    }
  }
}
